package com.wangdao.mall.controller.client;

import com.google.gson.Gson;
import com.wangdao.mall.model.*;
import com.wangdao.mall.service.GoodsService;
import com.wangdao.mall.service.GoodsServiceImp;
import com.wangdao.mall.utils.HttpUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author:HaiyangTian
 * @Description:
 * @Date: Created in 22:28 2020/5/5
 * @Modified By:
 */
@WebServlet("/api/mall/goods/*")
public class GoodsServlet extends HttpServlet {

    private GoodsService goodsService = new GoodsServiceImp();

    Gson gson = new Gson();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String uri = request.getRequestURI();
        String action = uri.replace("/api/mall/goods/", "");
        if ("askGoodsMsg".equals(action)){
            askGoodsMsg(request,response);
        }
    }

    /**
     * 发送提问的消息
     * @param request
     * @param response
     */
    private void askGoodsMsg(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        AskMessage message = gson.fromJson(requestBody, AskMessage.class);
        int row = goodsService.askGoodsMsg(message);
        Result result = new Result();
        if (row == 1){
            result.setCode(0);
        }else{
            result.setCode(10000);
            result.setMessage("留言失败");
        }
        response.getWriter().println(gson.toJson(result));
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String uri = request.getRequestURI();
        String action = uri.replace("/api/mall/goods/", "");
        if ("getGoodsByType".equals(action)){
            getGoodsByType(request,response);
        }else if ("getGoodsInfo".equals(action)){
            getGoodsInfo(request,response);
        }else if ("getGoodsMsg".equals(action)){
            getGoodsMsg(request,response);
        }
    }

    /**
     * 获取商品的提问和回答
     * @param request
     * @param response
     */
    private void getGoodsMsg(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        List<ClientMessage> messages = goodsService.getGoodsMsg(id);
        Result result = new Result();
        result.setCode(0);
        result.setData(messages);
        response.getWriter().println(gson.toJson(result));
    }

    /**
     * 获取商品的详情
     * @param request
     * @param response
     */
    private void getGoodsInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        ClientGoods goods = goodsService.getClientGoodsInfo(id);
        Result result = new Result();
        result.setCode(0);
        result.setData(goods);
        response.getWriter().println(gson.toJson(result));
    }

    /**
     * 通过分类显示商品
     * @param request
     * @param response
     */
    private void getGoodsByType(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String typeId = request.getParameter("typeId");
        List<Goods> goodsList = goodsService.getGoodsByType(typeId);
        Result result = new Result();
        result.setCode(0);
        result.setData(goodsList);
        response.getWriter().println(gson.toJson(result));
    }
}
